<template>
  <div class="headinformationstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <p style="color: #323233; font-size: 14px">标题图片</p>
    <!-- 头像 -->
    <div class="backgroundImg">
      <img
        v-if="!datas.titlePic"
        draggable="false"
        src="../../../assets/images/add.png"
        alt=""
        @click="uploadI('titlePic')"
      />
      <div v-else>
        <img draggable="false" :src="datas.titlePic" alt="" />
        <p @click="deleteupimg">删除</p>
      </div>
    </div>

    <div style="margin: 10px 0" />

    <!-- 表单 -->
    <el-form label-width="80px" :model="datas" size="small" :rules="rules">
      <!-- 简介 -->
      <el-form-item label="简介：" class="lef">
        <el-input v-model="datas.briefIntroduction" placeholder="" clearable maxlength="45"/>
      </el-form-item>

      <el-form-item class="lef" label="右侧图标" />
      <vuedraggable v-model="datas.iconList" v-bind="dragOptions">
        <transition-group>
          <section v-for="(item, index) in datas.iconList" :key="item + index" class="imgBanner">
            <i class="el-icon-circle-close" @click="deleteimg(index)" />
            <!-- 图片 -->
            <div>
              <div class="imagBox">
                <img class="imag" :src="item.iconPic" draggable="false" />
              </div>
            </div>
            <!-- 标题和链接 -->
            <div class="imgText">
              <div class="imgTextChild">
                <!-- 选择类型 -->
                <select-link
                  @selectThisLink="selectThisLink"
                  @clearThisLink="clearThisLink(index)"
                  :linktypeurl="item.url"
                  :theIndex="index"/>
              </div>
              <div class="isub">
                <span>是否U币:</span>
                <el-radio-group v-model="item.isub">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="0">否</el-radio>
                </el-radio-group>
              </div>
            </div>
          </section>
        </transition-group>
      </vuedraggable>

      <el-button v-if="datas.iconList.length < 3" class="uploadImg" type="primary" plain @click="uploadI('rightIcon')">
        <i class="el-icon-plus" />点击上传图标
      </el-button>
    </el-form>

    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>

<script>
import uploadimg from "../../uploadImg"; // 图片上传
import vuedraggable from "vuedraggable"; // 拖拽组件
import SelectLink from "../../selectLink";
export default {
  name: "Headformationstyle",
  components: { 
    uploadimg,
    vuedraggable,
    SelectLink
  },
  props: {
    datas: Object,
  },
  data() {
    return {
      danqian: "", // 当前选中的是头像还是右侧图标
      rules: {
        briefIntroduction: [{ required: true, message: "请输入简介", trigger: "blur" }],
      },
      dragOptions: {
        animation: 200,
      },
      isub: 0,
    };
  },
  methods: {
    inputFocus() {
      this.datas.briefIntroduction = "";
    },
    /* 上传图片 */
    uploadI(res) {
      this.danqian = res;
      this.$refs.upload.showUpload();
    },
    /* 删除图片 */
    deleteimg(index) {
      this.datas.iconList.splice(index, 1);
    },
    // 提交
    uploadInformation(res) {
      if (this.danqian == "titlePic") {
        this.datas.titlePic = res;
      }
      if (this.danqian == "rightIcon") {
        this.datas.iconList.push({
          /** 图标图片 */
          iconPic: res,
          isub: this.isub,
        });
      }
    },
    // 选择链接
    selectThisLink(resu) {
      this.datas.iconList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
        }
      });
    },
    clearThisLink (index) {
      this.datas.iconList[index].url = '';
    },
    // 删除标题图片
    deleteupimg() {
      this.datas.titlePic = "";
    },
  },
};
</script>

<style scoped lang="scss">
.headinformationstyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px 20px;
  box-sizing: border-box;
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }
  /* 背景图 */
  .backgroundImg {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    width: 60px;
    height: 60px;
    position: relative;
    background: #f2f4f6;

    img {
      width: 100%;
      height: auto;
    }

    p {
      position: absolute;
      left: 0;
      bottom: -10px;
      width: 100%;
      height: 20px;
      font-size: 12px;
      color: #fff;
      background: rgba(0, 0, 0, 0.5);
      text-align: center;
      line-height: 20px;
    }
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }
  .select-sp {
    display: flex;
    flex-wrap: wrap;
  }
  .isub {
    span {
      font-size: 14px;
      color: #666666;
    }
  }

  /* 卡片列表 */
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imagBox {
      position: relative;
      border-radius: 5px;
      overflow: hidden;
      cursor: pointer;
      .imag {
        width: 60px;
        height: 60px;
      }
      div {
        position: absolute;
        top: 0;
        width: 60px;
        line-height: 60px;
        border-radius: 5px;
        text-align: center;
        font-size: 12px;
        color: #fff;
        background-color: rgba(0, 0, 0, 0.3);
      }
    }

    /* 图片字 */
    .imgText {
      padding-left: 20px;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      justify-content: space-around;
      /* 图片字 */
      .imgTextChild {
        width: 100%;
        display: flex;
        box-sizing: border-box;
        justify-content: space-between;
        .fir-sele.el-select {
          width: 40%;
        }
      }
      .imgText-top {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        .imgText-top-r {
          flex: 1;
          text-align: center;
          span {
            margin-right: 10px;
          }
        }
        .el-input,
        .el-input--mini {
          flex: 1;
        }
      }
    }
  }
}
</style>
